package cn.cdut.yiwenqian.user.controller;

import java.io.File;
import java.math.BigDecimal;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import cn.cdut.yiwenqian.biz.interfaces.INewsManager;
import cn.cdut.yiwenqian.entity.CdutPostNews;
import cn.cdut.yiwenqian.entity.CdutPostUsers;
import cn.cdut.yiwenqian.entityVo.NewsVo;
import cn.cdut.yiwenqian.util.MyFileTool;
import cn.cdut.yiwenqian.util.MyPageToolVo;
@Controller
@Namespace(value="/news")
@InterceptorRefs(value = {@InterceptorRef("fileUploadStack")})
public class NewsManagerAction extends ActionSupport{
	
	@Resource
	private INewsManager iNewsManager;
	private Logger log= Logger.getLogger(NewsManagerAction.class);
	
	private String page;
	private String unumber;
    private String name;
    private String content;
	
	private File image;
	private String imageFileName;
	private String imageContentType;
	
	private File upload;
	private String uploadFileName;
	private String uploadContentType;

	@Action(value="fingNewsByAuthorId",results={@Result(name="success",location="/WEB-INF/jsp/user/MyNews.jsp")})
	public String fingNewsByAuthorId(){
		page=page==null?"1":page;
		CdutPostUsers user=(CdutPostUsers) ActionContext.getContext().getSession().get("user");
		MyPageToolVo<CdutPostNews> li=iNewsManager.fingNewsByAuthorId(user.getUnumber(), Integer.parseInt(page));
//		 li.getList().sort(new Comparator<CdutPostNews>() {
//
//			@Override
//			public int compare(CdutPostNews o1, CdutPostNews o2) {
//				if(o1.getBegindate().before(o2.getBegindate())){
//					return 1;
//				}else if(o1.getBegindate().after(o2.getBegindate())){
//					return -1;
//				}else{
//					return 0;
//				}
//				
//			}
//		});
		ServletActionContext.getRequest().setAttribute("page", li);
		return "success";
	}
	@Action(value="createNews", results={@Result(name="success",location="/WEB-INF/jsp/news/NewsInfoPage.jsp"),@Result(name="error",location="/WEB-INF/jsp/hello.jsp")})
	public String createNews() throws Exception{
		CdutPostUsers user=(CdutPostUsers) ActionContext.getContext().getSession().get("user");
		CdutPostNews news=new CdutPostNews();
		if(image!=null&&imageFileName!=null){
			news.setImage(MyFileTool.saveImageSourse(image, imageFileName));
			log.info(news.getImage());
		}else{
			ServletActionContext.getRequest().setAttribute("message", "图片上传失败，请重新创建消息！");
			return "error";
		}
		if(upload!=null&&uploadFileName!=null){
			news.setSource(MyFileTool.saveNewsSourse(upload, uploadFileName));
			log.info(news.getSource());
		}else{
			ServletActionContext.getRequest().setAttribute("message", "附件上传失败，请重新创建消息！");
			return "error";
		}
		news.setName(name);
		news.setCdutPostUsers(user);
		news.setContent(MyFileTool.createTxtFile(content));
		ServletActionContext.getRequest().setAttribute("news", iNewsManager.saveNewInformation(news));
		return "success";
	}
	@Action(value="lookUpNewsInformation",results={@Result(name="success",location="/WEB-INF/jsp/news/NewsInfoPage.jsp")})
	public String lookUpNewsInformation(){
		NewsVo news=null;
		try {
			news = iNewsManager.findNewsByUnumber(Integer.parseInt(unumber));
		} catch (NumberFormatException e) {
			throw e;
		} catch (Exception e) {
			ServletActionContext.getRequest().setAttribute("message", "内容获取失败！");
			return "error";
		}
			ServletActionContext.getRequest().setAttribute("news", news);
			return "success";
	}
	public String fingAllNews(){
		MyPageToolVo<CdutPostNews> li=iNewsManager.findAllNewsInSystem(Integer.parseInt(page));
		ServletActionContext.getRequest().setAttribute("newsList", li);
		return "success";
	}
	/**
	 * @return the iNewsManager
	 */
	public INewsManager getiNewsManager() {
		return iNewsManager;
	}
	/**
	 * @return the page
	 */
	public String getPage() {
		return page;
	}
	/**
	 * @return the unumber
	 */
	public String getUnumber() {
		return unumber;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @return the content
	 */
	public String getContent() {
		return content;
	}
	/**
	 * @return the upload
	 */
	public File getUpload() {
		return upload;
	}
	/**
	 * @return the uploadFileName
	 */
	public String getUploadFileName() {
		return uploadFileName;
	}
	/**
	 * @return the uploadContentType
	 */
	public String getUploadContentType() {
		return uploadContentType;
	}
	/**
	 * @param iNewsManager the iNewsManager to set
	 */
	public void setiNewsManager(INewsManager iNewsManager) {
		this.iNewsManager = iNewsManager;
	}
	/**
	 * @param page the page to set
	 */
	public void setPage(String page) {
		this.page = page;
	}
	/**
	 * @param unumber the unumber to set
	 */
	public void setUnumber(String unumber) {
		this.unumber = unumber;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @param content the content to set
	 */
	public void setContent(String content) {
		this.content = content;
	}
	/**
	 * @param upload the upload to set
	 */
	public void setUpload(File upload) {
		this.upload = upload;
	}
	/**
	 * @param uploadFileName the uploadFileName to set
	 */
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	/**
	 * @param uploadContentType the uploadContentType to set
	 */
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	/**
	 * @return the image
	 */
	public File getImage() {
		return image;
	}
	/**
	 * @return the imageFileName
	 */
	public String getImageFileName() {
		return imageFileName;
	}
	/**
	 * @return the imageContentType
	 */
	public String getImageContentType() {
		return imageContentType;
	}
	/**
	 * @param image the image to set
	 */
	public void setImage(File image) {
		this.image = image;
	}
	/**
	 * @param imageFileName the imageFileName to set
	 */
	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}
	/**
	 * @param imageContentType the imageContentType to set
	 */
	public void setImageContentType(String imageContentType) {
		this.imageContentType = imageContentType;
	}

	
}
